package com.cnswhy.cloud.server.system.controller;

import com.aliyun.oss.OSSClient;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.utils.Kits;
import com.cnswhy.cloud.common.redis.service.IdGeneratorService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;


/**
 * <p>
 * 前端控制器
 * 系统设置(t_base_settings)
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-06
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/upload")
@Api(value = "UploadFile", tags = "文件上传")
public class UploadFileController {
    @Autowired
    IdGeneratorService idGeneratorService;
    @PostMapping
    @GetMapping
    @RequestMapping("/uploadFile")
    @ResponseBody
    public R<String> detail(@RequestParam(value = "file", required = false) MultipartFile file,String bizType)
    {
        String imageName = null;
        if(StringUtils.isBlank(bizType)){
            imageName = idGeneratorService.genCommonIdString() + ".jpg";
        }else{
            imageName = bizType + idGeneratorService.genCommonIdString() + ".jpg";
        }

        String imgPath = updateLoadFileToOss( file, imageName);//新增相片到图片服务器
        log.info(imageName);
        /*JSONObject jsonObject = new JSONObject();
        jsonObject.put("pathUrl",imgPath);*/
        return R.success(imgPath);
    }

    public static void main(String[] args) {
        updateLoadFileToOss(null, "abc");
    }

    public static String updateLoadFileToOss( MultipartFile file, String fileName) {
        // Endpoint以杭州为例，其它Region请按实际情况填写。http://oss-cn-hangzhou.aliyuncs.com
        String endpoint = "http://oss-cn-shenzhen.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 http://ram.console.aliyun.com 创建RAM账号。

        String accessKeyId = "LTAIlBFDDNv8oSjE";
        String accessKeySecret = "BwRCJaY2QwAFDkSG5twZhJkp3FQNjr";
        String bucketName="leyoutuan";
        //fileName=userId+ Kits.Date.getyyyyMMddHHmmss()+ Kits.Random.getRandom(0,10000)+fileName;
        String Url="http://leyoutuan.oss-cn-shenzhen.aliyuncs.com/"+fileName;

        InputStream fis = null;
        try {
            fis = file.getInputStream();
            // 创建OSSClient实例。
            OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
            ossClient.putObject(bucketName, fileName, fis);
            // 关闭OSSClient。
            ossClient.shutdown();
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("加载异常:{}", ex.toString());
            return null;
        } finally {

            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    log.error("加载异常:{}", e);
                }
            }
        }
        return Url;
    }
}
